package com.cn.kabalifeuser.ui.order.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.LazyFragment;
import com.cn.kabalifeuser.callback.SchedulerTransformer;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.event.RxBus;
import com.cn.kabalifeuser.event.RxBusBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.order.activity.MallOrderAfterSaleDetailsActivity;
import com.cn.kabalifeuser.ui.order.activity.MallOrderDetailsActivity;
import com.cn.kabalifeuser.ui.order.bean.OrderListAfterSaleBean;
import com.cn.kabalifeuser.ui.order.bean.OrderListListBean;
import com.cn.kabalifeuser.utils.GlideUtil;
import com.cn.kabalifeuser.utils.RxToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Created by 晓林 on 2020/9/25.
 * function: 售后
 */
public class MallOrderAfterSaleFragments extends LazyFragment<MallFragmentslView, MallFragmentsPresenter> implements MallFragmentslView {
    @BindView(R.id.rv_notice)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private View notDataView;
    List<OrderListAfterSaleBean.ResultBean.DataBean> mList;
    OrderAdapter mAdapter;
    private int mId;

    int orderID = -1;

    public static MallOrderAfterSaleFragments getInstance(int mid) {
        MallOrderAfterSaleFragments fragment = new MallOrderAfterSaleFragments();
        fragment.mId = mid;
        return fragment;
    }

    @Override
    protected MallFragmentsPresenter createPresenter() {
        return new MallFragmentsPresenter();
    }

    @Override
    protected void onUserVisible() {
        pagehttp = 1;
        getListData();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.recyclerview_refresh;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getMe());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager3.setSmoothScrollbarEnabled(true);
        layoutManager3.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(layoutManager3);
        recyclerView.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new OrderAdapter(R.layout.fragment_order_mall_aftersale_item, mList, mId);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(mAdapter);
        notDataView = getLayoutInflater().inflate(R.layout.show_empty_view, (ViewGroup) recyclerView.getParent(), false);
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshLayout.autoRefresh();
            }
        });

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("id", mAdapter.getItem(position).getId());
                $startActivity(MallOrderAfterSaleDetailsActivity.class, bundle);
            }
        });
        initRefreshLayout();


        rxBusSubscription = RxBus.getDefault().toObservable(RxBusBean.class)
                .compose(new SchedulerTransformer<RxBusBean>())
                .subscribe(new Action1<RxBusBean>() {
                    @SuppressLint("WrongConstant")
                    @Override
                    public void call(RxBusBean rxBusBean) {
                        if (rxBusBean.getSign() == 1111 && rxBusBean.getId() == 1111) {
                            //详情操作成功以后。
                            pagehttp = 1;
                            getListData();
                        }
                    }
                });


    }


    /**
     * 刷新
     */
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    private void getListData() {
        mPresenter.onOrderAfterSaleListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        RxToast.error(msg);

    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onOrderListSuccess(OrderListListBean bean) {

    }

    @Override
    public void onOrderSetSuccess(MsgBean bean) {
    }

    @Override
    public void onOrderPaySuccess(String json) {
    }

    @Override
    public void onOrderAfterSaleListSuccess(OrderListAfterSaleBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }


    class OrderAdapter extends BaseQuickAdapter<OrderListAfterSaleBean.ResultBean.DataBean, BaseViewHolder> {

        int mId;

        public OrderAdapter(int layoutResId, @Nullable List<OrderListAfterSaleBean.ResultBean.DataBean> data, int mId) {
            super(layoutResId, data);
            this.mId = mId;
        }

        @Override
        protected void convert(BaseViewHolder helper, final OrderListAfterSaleBean.ResultBean.DataBean item) {
            GlideUtil.ImageLoad(this.mContext, item.getGood_img(), helper.getView(R.id.good_img));
            helper.setText(R.id.tv_total_amount, item.getOriginal_price())
                    .setText(R.id.tv_num, item.getNum())
                    .setText(R.id.tv_status, item.getStatus_txt())
                    .setText(R.id.tv_title, item.getTitle())
                    .setText(R.id.tv_spec_key, item.getSpec_key())
                    .setText(R.id.tv_money, item.getOriginal_price())
                    .setText(R.id.tv_number, String.valueOf(item.getGood_num()))
                    .setText(R.id.tv_good_num, "x " + item.getGood_num());
        }


    }
}




